#include "stdafx.h"
#include "AccountManagement.h"
#include "Lm.h"

Account::Account()
{
	m_StrUserName = "";
	m_StrPassword = "";
	m_StrFullName = "";
	m_StrDescription = "";
	m_StrGroup = "";
	iType = 1;
}

Account::~Account()
{
}

AccountManager::AccountManager()
{
}

AccountManager::~AccountManager()
{
}



int AccountManager::CreateAccount(Account *account,CString groupName)
{
	USER_INFO_2 *userInfo = new USER_INFO_2();
	userInfo->usri2_name = account->m_StrUserName.GetBuffer();
	userInfo->usri2_password = account->m_StrPassword.GetBuffer();
	userInfo->usri2_full_name = account->m_StrFullName.GetBuffer();
	userInfo->usri2_priv = account->iType;
	userInfo->usri2_home_dir = NULL;
	userInfo->usri2_comment = account->m_StrDescription.GetBuffer();
	userInfo->usri2_flags = UF_SCRIPT;
	userInfo->usri2_script_path = NULL;
	userInfo->usri2_acct_expires =  TIMEQ_FOREVER;
	DWORD res = NetUserAdd(NULL, 2, (LPBYTE)userInfo, NULL);
	if(res == 0)
	{
		LOCALGROUP_MEMBERS_INFO_3 member;
		member.lgrmi3_domainandname = userInfo->usri2_name;
		res = NetLocalGroupAddMembers(NULL,groupName.GetBuffer(),3,(LPBYTE)&member,1);
		if(res == 0)
			return res;
	}
	return res;
}

bool AccountManager::ChangeGroup(CString username,CString groupName)
{
	CString oldGroupName;
	if(groupName == _T("Vip"))
	{
		oldGroupName = _T("Normal");
	}
	else
	{
		oldGroupName = _T("Vip");
	}
	LOCALGROUP_MEMBERS_INFO_3 member;
	member.lgrmi3_domainandname = username.GetBuffer();
	NetLocalGroupDelMembers(NULL,oldGroupName.GetBuffer(),3,(LPBYTE)&member,1);
	DWORD res = NetLocalGroupAddMembers(NULL,groupName.GetBuffer(),3,(LPBYTE)&member,1);
	if(res == 0)
		return true;
	else
		return false;
}



std::vector<Account*> AccountManager::GetGroupAccountList(CString groupName)
{
	std::vector<Account*> lstAccount;
	LOCALGROUP_MEMBERS_INFO_1* user = NULL;
	LOCALGROUP_MEMBERS_INFO_1* tempuser;	
	
	
	DWORD level = 2;
	DWORD filter = 2;
	BYTE* iAccountNum;
	DWORD iEntriesRead = 0;
	DWORD iTotalEntries = 0;
	DWORD iResume = 0;

	int iRes =  NetLocalGroupGetMembers(NULL,groupName.GetBuffer(),1,(LPBYTE*)&user,MAX_PREFERRED_LENGTH,&iEntriesRead,&iTotalEntries,&iResume);
	tempuser = user;

	for(int i = 0;i < iEntriesRead;i++)
	{
		Account* acc = new Account();
		*acc =	GetUserInfo(tempuser->lgrmi1_name);
		acc->m_StrGroup = groupName;	
		lstAccount.push_back(acc);
		tempuser++;
	}

	if (user != NULL)
	{
		NetApiBufferFree(user);
		user = NULL;
	}
	return lstAccount;
}
std::vector<Account*> AccountManager::GetAccountList()
{
	std::vector<Account*> lstAccount;
	std::vector<Account*> lstAccountTemp;
	LOCALGROUP_MEMBERS_INFO_1* user = NULL;
	LOCALGROUP_MEMBERS_INFO_1* tempuser;	
	
	
	DWORD level = 2;
	DWORD filter = 2;
	BYTE* iAccountNum;
	DWORD iEntriesRead = 0;
	DWORD iTotalEntries = 0;
	DWORD iResume = 0;
	//Lay danh sach tai khoan VIP
	lstAccount =  GetGroupAccountList(_T("Vip"));
	//Lay danh sach tai khoan thuong
	lstAccountTemp =  GetGroupAccountList(_T("Normal"));
	for(int i = 0; i < lstAccountTemp.size(); i++)
		lstAccount.push_back(lstAccountTemp.at(i));
	return lstAccount;
}

int AccountManager::DeleteAccount(Account* account)
{
	DWORD error = NetUserDel(NULL, account->m_StrUserName);

	return error;
}

int AccountManager::ChangeAccountInfo(Account* account)
{
	/*
	USER_INFO_2 *userInfo = new USER_INFO_2();
	DWORD error = 0;
	if(NetUserGetInfo(NULL, account->m_StrUserName.GetBuffer(), 2, (LPBYTE*)&userInfo) != 0)
	{
		return false;
	}
	userInfo->usri2_name = account->m_StrUserName.GetBuffer();
	userInfo->usri2_full_name = account->m_StrFullName.GetBuffer();
	userInfo->usri2_comment = account->m_StrDescription.GetBuffer();
	userInfo->usri2_priv = account->iType;
	userInfo->usri2_password = NULL;
	if(NetUserSetInfo(NULL,userInfo->usri2_name,userInfo->usri2_priv,(LPBYTE)userInfo,&error) ==  NERR_Success)
	{
		NetApiBufferFree(userInfo);
		return true;
	}
	if(ERROR_ACCESS_DENIED == error)
		return false;
	NetApiBufferFree(userInfo);
	return false;
    */
	//

	USER_INFO_2 *userInfo = new USER_INFO_2();
	DWORD error = 0;
	DWORD level;
	DWORD res;
	error = NetUserGetInfo(NULL, account->m_StrUserName, 2, (LPBYTE*)&userInfo);	
	if( error != 0)
	{
		return error;
	}
    else
    {
		level = userInfo->usri2_priv;
		userInfo->usri2_full_name = account->m_StrFullName.GetBuffer();
		userInfo->usri2_password = NULL;
		userInfo->usri2_comment = account->m_StrDescription.GetBuffer();
        res = NetUserSetInfo(NULL, account->m_StrUserName, 2, (LPBYTE)userInfo, &error);
    }

	NetApiBufferFree(userInfo);
	if (res != 0)
    {
        return error;
    }	
	return 0;

	//
}

Account AccountManager::GetUserInfo(CString username)
{
	USER_INFO_2 *userInfo = new USER_INFO_2();
	DWORD error = 0;
	DWORD level;
	DWORD res;
	Account acc;
    if (NetUserGetInfo(NULL, username, 2, (LPBYTE*)&userInfo) == 0)
    {    		
		acc.iType = userInfo->usri2_priv;
		acc.m_StrUserName = userInfo->usri2_name;
		acc.m_StrFullName = userInfo->usri2_full_name;
		acc.m_StrDescription = userInfo->usri2_comment;
    }

	NetApiBufferFree(userInfo);
	return acc;
}

CString AccountManager::getGroupName(CString username)
{
	GROUP_USERS_INFO_0* group = NULL;
	DWORD dwEntriesRead = 0;
    DWORD dwTotalEntries = 0;
	CString groupName = _T("");
	if(NetUserGetLocalGroups(NULL,username,0, LG_INCLUDE_INDIRECT,(LPBYTE*)&group, MAX_PREFERRED_LENGTH,&dwEntriesRead, &dwTotalEntries) ==  NERR_Success)
	{
		for (int i = 0; i < dwEntriesRead; i++)
			groupName = group->grui0_name;
	}
	return groupName;
}